Answer:

In the body of the actionPerformed() method.

Changing the Color of a Frame

We will fill in the actionPerformed() body so that clicking the button changes the color of the frame. To change the color of the content pane of a frame, use the method setBackground(Color c). You can use one of the pre-defined colors like this:

getContentPane().setBackground( Color.red )

Other pre-defined colors are Color.green, Color.blue, Color.yellow, and so on. (Look in your Java documentation under the class Color to find more. Here is the interesting part of our program:

class ButtonFrame extends JFrame implements ActionListener
{
  JButton bChange ;

  // constructor   
  public ButtonFrame() 
  {
    bChange = new JButton("Click Me!"); 
    getContentPane().setLayout( new FlowLayout() );  
    
    // register this ButtonFrame object as the listener for its own JButton.
    bChange.addActionListener( this ); 
    
    getContentPane().add( bChange ); 
    setDefaultCloseOperation( WindowConstants.EXIT_ON_CLOSE );    
  }
   
  public void actionPerformed( ActionEvent evt)
  {
    getContentPane(). ;
    repaint();  // ask the system to paint the screen.
  }
  
}

The repaint() will be explained in the next page.

QUESTION 16:

Fill in the blank so that the program changes the frame to blue when the button is clicked.